package com.lgmshare.component.app;

import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lgmshare.component.logger.Logger;

/**
 * dialogFragment基类
 *
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2017/4/1 14:39
 */
public abstract class LaraDialogFragment extends DialogFragment {

    protected final String TAG = LaraDialogFragment.this.getClass().getName();

    private View mRootView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Logger.v(TAG, "onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Logger.v(TAG, "onCreate");
        initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Logger.v(TAG, "onCreateView");
        int resId = onBindLayoutResId();
        if (resId > 0) {
            mRootView = inflater.inflate(resId, container, false);
            initView();
            return mRootView;
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Logger.v(TAG, "onActivityCreated");
        initLoad();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Logger.v(TAG, "onDetach");
    }

    public <T extends View> T findViewById(@IdRes int id) {
        if (mRootView == null) {
            return null;
        }
        return mRootView.findViewById(id);
    }

    /**
     * 绑定布局资源文件ID
     * use with {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
     *
     * @return 资源布局文件ID
     */
    protected abstract int onBindLayoutResId();

    /**
     * 初始化Intent Data
     * use with {@link #onCreate(Bundle)}
     */
    protected abstract void initData();

    /**
     * 初始化View
     * use with {@link #onBindLayoutResId()}
     */
    protected abstract void initView();

    /**
     * 初始化数据加载
     * use with {@link #onResume()}
     */
    protected abstract void initLoad();
}
